/****************************************************
文件：DSMonoController.cs
作者：UnitySir
邮箱：zouajck@foxmail.com
日期：2021/03/22 20:24:37
功能：Nothing
*****************************************************/

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace DSFramework
{
    public class MonoComponent : DSComponent
    {
        private static Dictionary<string, Action> UpdateDic = new Dictionary<string, Action>();
        private static Dictionary<string, Action> LateUpdateDic = new Dictionary<string, Action>();
        private static Dictionary<string, Action> FixedUpdateDic = new Dictionary<string, Action>();

        #region Update

        public void AddUpdate(string funName, Action fun)
        {
            try
            {
                UpdateDic.Add(funName, fun);
            }
            catch
            {
                Debug.LogError("Update名称重复");
            }
        }

        public void DelUpdate(string funName)
        {
            if (UpdateDic.ContainsKey(funName))
            {
                UpdateDic.Remove(funName);
            }
        }

        #endregion

        #region LateUpdate

        public void AddLateUpdate(string funName, Action fun)
        {
            try
            {
                LateUpdateDic.Add(funName, fun);
            }
            catch
            {
                Debug.LogError("LateUpdate名称重复");
            }
        }

        public void DelLateUpdate(string funName)
        {
            if (LateUpdateDic.ContainsKey(funName))
            {
                LateUpdateDic.Remove(funName);
            }
        }

        #endregion

        #region FixedUpdate

        public void AddFixedUpdate(string funName, Action fun)
        {
            try
            {
                FixedUpdateDic.Add(funName, fun);
            }
            catch
            {
                Debug.LogError("FixedUpdate名称重复");
            }
        }

        public void DelFixedUpdate(string funName)
        {
            if (FixedUpdateDic.ContainsKey(funName))
            {
                FixedUpdateDic.Remove(funName);
            }
        }

        #endregion

        private void Update()
        {
            var enumerator = UpdateDic.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    enumerator.Current.Value.Invoke();
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }

        private void LateUpdate()
        {
            var enumerator = LateUpdateDic.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    enumerator.Current.Value.Invoke();
                }
            }
            catch
            {
                // ignored
            }
        }

        private void FixedUpdate()
        {
            var enumerator = FixedUpdateDic.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    enumerator.Current.Value.Invoke();
                }
            }
            catch
            {
                // ignored
            }
        }
    }
}